import Context from "../Context";
import PopupWindow from "../PopupWindow";
import {WindowName} from "../reflect/WindowName";
import isString = cc.js.isString;
import {DialogContent} from "./WindowContent";

const {ccclass, property} = cc._decorator;

/**
 * Dialog 式弹窗.
 */

@ccclass
@WindowName("Dialog")
export default class Dialog extends PopupWindow {

    // 提供简便方法用于弹出弹窗.
    public static pop<T extends DialogContent>(view: { prototype: T }): Promise<T> {
        return Context.get().popup(Dialog).then((dialog) => {
            dialog.setBtnEnable(false, false);
            return dialog.setContentView(view);
        });
    }

    @property(cc.Label)
    public title: cc.Label = null;

    @property(cc.Node)
    public titleBar: cc.Node = null;

    @property(cc.Node)
    public closeBtn: cc.Node = null;

    @property(cc.Node)
    public okBtn: cc.Node = null;

    @property(cc.Node)
    public cancelBtn: cc.Node = null;

    @property(cc.Label)
    public okText: cc.Label = null;

    @property(cc.Label)
    public cancelText: cc.Label = null;

    @property(cc.Node)
    public btnGroup: cc.Node = null;

    public start() {
        if (this.closeBtn) {
            this.setClickListener(this.closeBtn, ((ev) => {
                this.finish();
                ev.stopPropagation();
            }));
        }
    }

    public setTitle(title: string) {
        this.title.string = title;
        this.setTitleBarEnable(true);
    }

    public setTitleColor(color: string | cc.Color) {
        if (isString(color)) {
            this.title.node.color = cc.Color.BLACK.fromHEX(color as string)
        } else {
            this.title.node.color = color as cc.Color
        }
    }

    public setBackgroundColor(color: string | cc.Color) {
        if (isString(color)) {
            this.contentView.color = cc.Color.BLACK.fromHEX(color as string)
        } else {
            this.contentView.color = color as cc.Color
        }
    }

    public setTitleBarEnable(enable: boolean) {
        if (this.titleBar.active != enable) {
            this.titleBar.active = enable;
            this.adjustHeight(this.dialogContent);
        }
    }

    public setBtnText(ok: string, cancel?: string) {
        if (ok) {
            this.okText.string = ok;
        }
        if (cancel) {
            this.cancelText.string = cancel;
        }
    }

    public setBtnEnable(okEnable: boolean, cancelEnable: boolean) {
        this.okBtn.active = okEnable || false;
        this.cancelBtn.active = cancelEnable || false;
        this.btnGroup.active = okEnable || cancelEnable;
        if (this.dialogContent) {
            this.adjustHeight(this.dialogContent);
        }
    }

    public setOkClickListener(callback: (dialog: Dialog) => void) {
        this.setClickListener(this.okBtn, ((ev) => {
            callback(this);
            this.finish();
        }));
    }

    public setOnCancelClickListener(callback: (dialog: Dialog) => void) {
        this.setClickListener(this.cancelBtn, ((ev) => {
            callback(this);
            this.finish();
        }));
    }

    // 调整Dialog的高度,自适应.
    protected adjustHeight(child: cc.Node | cc.Component) {
        let contentHeight = 0;
        if (child) {
            const node = child instanceof cc.Node ? child : child.node;
            node.width = this.contentView.width;
            this.content.height = node.height;
        } else {
            this.content.height = 100;
        }

        contentHeight += this.content.height;

        if (this.titleBar.active) {
            contentHeight += this.titleBar.height;
        }
        if (this.btnGroup.active) {
            contentHeight += this.btnGroup.height;
        }
        contentHeight += 40;
        this.contentView.height = contentHeight;
    }
}
